package sale;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ViewSaleList extends JPanel{
    JLabel byear,bmonth,bday,breminder,btitle;
    JComboBox<String> cyear,cmonth,cday;
    JTextField tyear,tmonth,tday;
    JButton bbegin;
    Box box;
    JPanel p1;
    private Vector rowData, columnName;
    private JTable jt = null;
    private JScrollPane jsp = null;
    public ViewSaleList(){
        JLabel btitlebig = new JLabel("查看销售列表            ");
        btitlebig.setFont(new   java.awt.Font("楷体", 1, 20));
        columnName=new Vector();
        columnName.add("流水号");
        columnName.add("图书编号");
        columnName.add("员工号");
        columnName.add("会员号");
        columnName.add("销售日期");
        columnName.add("售价");
        columnName.add("数量");
        btitle=new JLabel("  ",JLabel.LEFT);
        btitle.setFont(new java.awt.Font("楷体", 1, 15));
        btitle.setBackground(new Color(100,100,100));//设置背景色
        p1=new JPanel();
        box=Box.createVerticalBox();
        //setLayout( new BorderLayout() );
        breminder=new JLabel(" 销售日期：     ");
        byear=new JLabel("     年         ");
        bmonth=new JLabel("     月         ");
        bday=new JLabel("     日         ");
        tyear=new JTextField(5);
        tmonth=new JTextField(5);
        tday=new JTextField(5);
        cyear = new JComboBox<String>();
        cmonth = new JComboBox<String>();
        cday = new JComboBox<String>();
        bbegin=new JButton("确定");
        cyear.setModel(new DefaultComboBoxModel<String>(new String[] {"","2017","2016","2015"}));
        cmonth.setModel(new DefaultComboBoxModel<String>(new String[] {"","01","02","03","04","05","06",
                "07","08","09","10","11","12"}));
        cday.setModel(new DefaultComboBoxModel<String>(new String[] {"","01","02","03","04","05","06",
                "07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23",
                "24","25","26","27","28","29","30"}));
        Box box1=Box.createHorizontalBox();
        box1.add(breminder);
        box1.add(cyear);
        box1.add(tyear);
        box1.add(byear);
        box1.add(cmonth);
        box1.add(tmonth);
        box1.add(bmonth);
        box1.add(cday);
        box1.add(tday);
        box1.add(bday);
        box1.add(bbegin);
        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        validate(); // validate layout
        box.add(Box.createVerticalStrut(15));
        box.add(btitlebig);
        box.add(Box.createVerticalStrut(15));
        box.add(box1);
        p1.setSize(200, 200);
        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        validate();
        box.add(Box.createVerticalStrut(30));
        box.add(btitle);
        box.add(Box.createVerticalStrut(15));
        box.add(p1);
        this.add(box);
        cyear.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        tyear.setText((String) cyear.getSelectedItem());//设置文本框的内容
                    }//设置事件响应器

                });
        cmonth.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        tmonth.setText((String) cmonth.getSelectedItem());//设置文本框的内容
                    }//设置事件响应器

                });
        cday.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        tday.setText((String) cday.getSelectedItem());//设置文本框的内容
                    }//设置事件响应器

                });
        //box1.setPreferredSize(new Dimension(350, 100));
        //box2.setPreferredSize(new Dimension(300, 150));

        bbegin.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            DisplayRecord();
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void DisplayRecord() throws ParseException{
        String syear=tyear.getText();
        String smonth=tmonth.getText();
        String sday=tday.getText();
        String sdate;
        if(sday.equals("")){
            if(smonth.equals("")){
                sdate=syear;
            }
            else
                sdate=syear+'-'+smonth;
        }
        else
            sdate=syear+'-'+smonth+'-'+sday;

        try {
            String names2[] = {};
            //多条数据时处理方式
            String sqlString=new String("select Sno ,Bno ,Wno ,Vno ,Sdate ,Sprice,Snum " +
                    "from sale " +
                    "where Sdate like '"+sdate+"%"+"'");
            //System.out.print(sqlString);
            SaleManager salemanager=new  SaleManager();
            List<Sale> list=salemanager.Query(sqlString);
            StringBuffer results=new StringBuffer();
            rowData=new Vector();
            for (int i=0;i<list.size();i++){
                Vector<String> line1 = new Vector<String>();
                line1.add(String.valueOf(list.get(i).getSno()));
                line1.add(String.valueOf(list.get(i).getBno()));
                line1.add(String.valueOf(list.get(i).getWno()));
                line1.add(String.valueOf(list.get(i).getVno()));
                line1.add(String.valueOf(list.get(i).getSdate()));
                line1.add(String.valueOf(list.get(i).getSprice()));
                line1.add(String.valueOf(list.get(i).getSnum()));
                rowData.add(line1);
            }

            String outputdate;
            if(sday.equals("")){
                if(smonth.equals("")){
                    outputdate=syear+"年";
                }
                else
                    outputdate=syear+"年"+smonth+"月";
            }
            else
                outputdate=syear+"年"+smonth+"月"+sday+"日";
            btitle.setText(outputdate+"的销售列表为:        ");

            jt = new JTable(rowData, columnName);
            jsp = new JScrollPane(jt);
            p1.removeAll();
            p1.add( jsp);
            setSize( 300, 100 );  // set window size

        }
        catch ( SQLException sqlException ) {
            JOptionPane.showMessageDialog( null, sqlException.getMessage(),
                    "Database Error", JOptionPane.ERROR_MESSAGE );
        }
    }

}
